﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class UserSearcher
    {
        public string UserName { get; set; }
        public string RoleID { get; set; }
        public string Tel { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }

    public class EntityUser
    {
        public string ID { get; set; }
        [Required]
        public string Roles { get; set; }
        [Required]
        [MinLength(6, ErrorMessage = "长度最少为6个字符")]
        [MaxLength(20, ErrorMessage = "长度最大为20个字符")]
        public string UserName { get; set; }
        [Required]
        [MinLength(6, ErrorMessage = "长度最少为6个字符")]
        [MaxLength(20, ErrorMessage = "长度最大为20个字符")]
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }
        public string Name { get; set; }
        [Required]
        public string Status { get; set; }
        public string Email { get; set; }
        public Nullable<int> Integral { get; set; }
    }

    public class EntityUserInfo
    {
        public string ID { get; set; }
        public string Question { get; set; }
        public string Answer { get; set; }
        public string RealName { get; set; }
        public string County { get; set; }
        public string Province { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
        public string PostCode { get; set; }
        public string QQ { get; set; }
        public string Tel { get; set; }
        public string Phone { get; set; }
        public Nullable<bool> Gender { get; set; }
        public Nullable<System.DateTime> Birthday { get; set; }
        public string Photo { get; set; }
        public string Introduction { get; set; }
    }


    public class UserModel : BaseModel
    {
        public UserSearcher Searcher { get; set; }
        public string RoleList { get; set; }
        public string ProvinceList { get; set; }
        public string CityList { get; set; }
        public string CountyList { get; set; }
        public EntityUser Entity { get; set; }
        public EntityUserInfo EntityInfo { get; set; }

        public UserModel()
        {
            Searcher = new UserSearcher();
            RoleList = "[]";
            ProvinceList = "[]";
            CityList = "[]";
            CountyList = "[]";
        }

        public void InitList(bool defaultText = true)
        {
            var rp = new UserRepository();
            var data = rp.GetRoleList().Select(x => new { text = x.RoleName, value = x.ID }).ToList();
            if (defaultText == true)
                data.Insert(0, new { text = "请选择", value = "" });
            RoleList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(data);
        }

        public void InitProvince()
        {
            var rp = new UserRepository();
            var data = rp.GetProvince().Select(x => new { text = x.ZoneName, value = x.ID }).ToList();
            data.Insert(0, new { text = "请选择", value = string.Empty });
            ProvinceList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(data);
            if (EntityInfo != null && !string.IsNullOrEmpty(EntityInfo.Province))
            {
                var list = rp.GetZoneByParentID(EntityInfo.Province).Select(x => new { text = x.ZoneName, value = x.ID }).ToList(); ;
                CityList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(list);
            }
            if (EntityInfo != null && !string.IsNullOrEmpty(EntityInfo.City))
            {
                var list = rp.GetZoneByParentID(EntityInfo.City).Select(x => new { text = x.ZoneName, value = x.ID }).ToList(); ;
                CountyList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(list);
            }
        }

        public void InitEntity(string id)
        {
            var rp = new UserRepository();
            var user = rp.GetUserByID(id);
            string rids = string.Empty;
            foreach (var item in user.TP_Role)
            {
                rids += item.ID + ",";
            }
            if (!string.IsNullOrEmpty(rids))
                rids = rids.Substring(0, rids.Length - 1);
            Entity = new EntityUser
            {
                ID = user.TP_Users.ID,
                Email = user.TP_Users.Email,
                Integral = user.TP_Users.Integral,
                Password = user.TP_Users.Password,
                ConfirmPassword = user.TP_Users.Password,
                Roles = rids,
                Status = user.TP_Users.Status,
                UserName = user.TP_Users.UserName,
                Name = user.TP_Users.Name
            };
            if (user.TP_UserInfo != null)
            {
                EntityInfo = new EntityUserInfo
                {
                    ID = user.TP_UserInfo.ID,
                    Address = user.TP_UserInfo.Address,
                    Answer = user.TP_UserInfo.Answer,
                    Birthday = user.TP_UserInfo.Birthday,
                    City = user.TP_UserInfo.City,
                    County = user.TP_UserInfo.County,
                    Gender = user.TP_UserInfo.Gender,
                    Introduction = user.TP_UserInfo.Introduction,
                    Phone = user.TP_UserInfo.Phone,
                    Photo = user.TP_UserInfo.Photo,
                    PostCode = user.TP_UserInfo.PostCode,
                    Province = user.TP_UserInfo.Province,
                    QQ = user.TP_UserInfo.QQ,
                    Question = user.TP_UserInfo.Question,
                    Tel = user.TP_UserInfo.Tel,
                    RealName = user.TP_UserInfo.RealName,
                };
            }
        }

        public PagedList<UserList> GetUserList()
        {
            var rp = new UserRepository();
            var data = rp.GetUserList(Searcher.UserName, Searcher.RoleID, Searcher.Tel, Searcher.Phone, Searcher.Email, Searcher.StartDate, Searcher.EndDate, PageIndex, PageSize);
            return data;
        }

        public void DoAdd()
        {
            var rp = new UserRepository();
            var user = new TP_Users();
            user.Email = Entity.Email;
            user.CreateBy = "sys";
            user.CreateTime = DateTime.Now;
            user.Integral = Entity.Integral;
            user.Password = Entity.Password;
            user.Status = Entity.Status;
            user.UserName = Entity.UserName;
            user.Name = Entity.Name;
            TP_UserInfo userinfo = null;

            if (!string.IsNullOrEmpty(EntityInfo.Address) ||
                !string.IsNullOrEmpty(EntityInfo.Answer) ||
                EntityInfo.Birthday != null ||
                EntityInfo.Gender != true ||
                !string.IsNullOrEmpty(EntityInfo.Introduction) ||
                !string.IsNullOrEmpty(EntityInfo.Phone) ||
                !string.IsNullOrEmpty(EntityInfo.Photo) ||
                !string.IsNullOrEmpty(EntityInfo.PostCode) ||
                !string.IsNullOrEmpty(EntityInfo.QQ) ||
                !string.IsNullOrEmpty(EntityInfo.Question) ||
                !string.IsNullOrEmpty(EntityInfo.Tel) ||
                !string.IsNullOrEmpty(EntityInfo.RealName))
            {
                userinfo = new TP_UserInfo
                {
                    Address = EntityInfo.Address,
                    Answer = EntityInfo.Answer,
                    Birthday = EntityInfo.Birthday,
                    City = EntityInfo.City,
                    County = EntityInfo.County,
                    Gender = EntityInfo.Gender,
                    ID = Guid.NewGuid().ToString(),
                    Introduction = EntityInfo.Introduction,
                    Phone = EntityInfo.Phone,
                    Photo = EntityInfo.Photo,
                    PostCode = EntityInfo.PostCode,
                    Province = EntityInfo.Province,
                    QQ = EntityInfo.QQ,
                    Question = EntityInfo.Question,
                    Tel = EntityInfo.Tel,
                    RealName = EntityInfo.RealName,
                    CreateBy = "sys",
                    CreateTime = DateTime.Now
                };
                user.UserInfoID = userinfo.ID;
            }
            var userroles = new List<TP_UserRoles>();
            foreach (var item in Entity.Roles.Split(','))
            {
                userroles.Add(new TP_UserRoles { ID = Guid.NewGuid().ToString(), RoleID = item, UserID = user.ID });
            }
            rp.AddUser(user, userroles, userinfo, null);
        }

        public void DoEdit()
        {
            var rp = new UserRepository();
            var user = new TP_Users();
            user.ID = Entity.ID;
            user.Email = Entity.Email;
            user.CreateTime = DateTime.Now;
            user.Integral = Entity.Integral;
            user.Password = Entity.Password;
            user.Status = Entity.Status;
            user.UserName = Entity.UserName;
            user.Name = Entity.Name;
            TP_UserInfo userinfo = null;
            if (string.IsNullOrEmpty(EntityInfo.ID))
            {
                if (!string.IsNullOrEmpty(EntityInfo.Address) ||
                !string.IsNullOrEmpty(EntityInfo.Answer) ||
                EntityInfo.Birthday != null ||
                EntityInfo.Gender != true ||
                !string.IsNullOrEmpty(EntityInfo.Introduction) ||
                !string.IsNullOrEmpty(EntityInfo.Phone) ||
                !string.IsNullOrEmpty(EntityInfo.Photo) ||
                !string.IsNullOrEmpty(EntityInfo.PostCode) ||
                !string.IsNullOrEmpty(EntityInfo.QQ) ||
                !string.IsNullOrEmpty(EntityInfo.Question) ||
                !string.IsNullOrEmpty(EntityInfo.Tel) ||
                !string.IsNullOrEmpty(EntityInfo.RealName))
                {
                    userinfo = new TP_UserInfo
                    {
                        Address = EntityInfo.Address,
                        Answer = EntityInfo.Answer,
                        Birthday = EntityInfo.Birthday,
                        City = EntityInfo.City,
                        County = EntityInfo.County,
                        Gender = EntityInfo.Gender,
                        ID = Guid.NewGuid().ToString(),
                        Introduction = EntityInfo.Introduction,
                        Phone = EntityInfo.Phone,
                        Photo = EntityInfo.Photo,
                        PostCode = EntityInfo.PostCode,
                        Province = EntityInfo.Province,
                        QQ = EntityInfo.QQ,
                        Question = EntityInfo.Question,
                        Tel = EntityInfo.Tel,
                        RealName = EntityInfo.RealName,
                        CreateTime = DateTime.Now
                    };
                    user.UserInfoID = userinfo.ID;
                }
            }
            else
            {
                userinfo = new TP_UserInfo
                {
                    Address = EntityInfo.Address,
                    Answer = EntityInfo.Answer,
                    Birthday = EntityInfo.Birthday,
                    City = EntityInfo.City,
                    County = EntityInfo.County,
                    Gender = EntityInfo.Gender,
                    ID = EntityInfo.ID,
                    Introduction = EntityInfo.Introduction,
                    Phone = EntityInfo.Phone,
                    Photo = EntityInfo.Photo,
                    PostCode = EntityInfo.PostCode,
                    Province = EntityInfo.Province,
                    QQ = EntityInfo.QQ,
                    Question = EntityInfo.Question,
                    Tel = EntityInfo.Tel,
                    RealName = EntityInfo.RealName,
                    CreateTime = DateTime.Now
                };
                user.UserInfoID = userinfo.ID;
            }
            var userroles = new List<TP_UserRoles>();
            foreach (var item in Entity.Roles.Split(','))
            {
                userroles.Add(new TP_UserRoles { ID = Guid.NewGuid().ToString(), RoleID = item, UserID = user.ID });
            }
            rp.EditUser(user, userroles, userinfo);
        }

        public void Disable(string id)
        {
            var rp = new UserRepository();
            rp.DisableUser(id, ((int)SysEnum.UserStatus.Disable).ToString());
        }

    }
}